
define(function (require, exports, module) {
	var roomTypesPriceCalendarComp = require("../component/roomTypesPriceCalendarComp");
	var aw = require("ajaxwrapper");
	var roomTypesPriceCalendarComp = require("../component/roomTypesPriceCalendarComp");
	var roomTypeDataSvc = require("../store/roomTypeDataSvc");
	var moment = require("moment");
	
    module.exports = {
    	saveAction:function(data, widget){
    		data.week = data.week.join(",");
    		roomTypeDataSvc.save(data,function(data){
    			var subnav = widget.get("subnav");
				subnav.hide(["return"]).show(["add","servicepoint","buiding","roomtypes"]);
                widget.get("monthRange").show();
                $(".J-calendar").show();
                widget.get("form").reset();
                widget.hide([".J-form"]);
                roomTypesPriceCalendarComp.setCalendarData(widget);
    		});
    	},
    	changeDate:function(form){
    		var startDate = form.getValue("startDate");
    		var endDate = form.getValue("endDate");
    		var oneDayTime = 1000*60*60*24;
    		if(startDate && endDate && (endDate-startDate)< (oneDayTime*6)){
    			$("input[name='week']",form.element).attr("disabled","disabled");
   				$("input[name='week']",form.element).prop("checked",false);
    			
    			var start = moment(startDate).format("YYYY-MM-DD");
    			var end = moment(endDate).format("YYYY-MM-DD");
    			var dateArr = this.dateScope(start,end);
    			
    			for(var i=0;i<dateArr.length;i++){
    				var iWeek = moment(dateArr[i]).day();
    				if(iWeek === 0){
    					iWeek = 7;
    				}
    				iWeek = iWeek -1;
    				$($("input[name='week']",form.element)[iWeek]).removeAttr("disabled");
    				$($("input[name='week']",form.element)[iWeek]).prop("checked",true);
    			}
    		}else{
    			$("input[name='week']",form.element).removeAttr("disabled");
    			$("input[name='week']",form.element).prop("checked",true);
    		}
    		
    	},
    	dateScope:function(value1, value2) {  
    	    var getDate = function(str) {  
    	        var tempDate = new Date();  
    	        var list = str.split("-");  
    	        tempDate.setFullYear(list[0]);  
    	        tempDate.setMonth(list[1] - 1);  
    	        tempDate.setDate(list[2]);  
    	        return tempDate;  
    	    }  
    	    var date1 = getDate(value1);  
    	    var date2 = getDate(value2);
    	    var dateArr = [];
    	    
    	    if(value1 != value2){
    	    	if (date1 > date2) {  
        	        var tempDate = date1;  
        	        date1 = date2;  
        	        date2 = tempDate;  
        	    }
        	    date1.setDate(date1.getDate() + 1);  
        	    
        	    var i = 0;  
        	    while (!(date1.getFullYear() == date2.getFullYear()  
        	            && date1.getMonth() == date2.getMonth() && date1.getDate() == date2  
        	            .getDate())) {  
        	         var dayStr =date1.getDate().toString();  
        	            if(dayStr.length ==1){  
        	                dayStr="0"+dayStr;  
        	            }  
        	        dateArr[i] = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-"  
        	                + dayStr;  
        	        i++;  
        	        date1.setDate(date1.getDate() + 1);  
        	    }  
        	    dateArr.unshift(value1);
        	    dateArr.push(value2);
        	    return dateArr; 
    	    }else{
    	    	var dayStr =date1.getDate().toString();  
	            if(dayStr.length ==1){  
	                dayStr="0"+dayStr;  
	            }  
	            dateArr[0] = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-"  
	                + dayStr;
	            return dateArr;
    	    }
    	}
    };
});
